import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import 'ab.dart';

class AWidget extends StatefulWidget {
  @override
  _AWidgetState createState() => _AWidgetState();
}

class _AWidgetState extends State<AWidget> {
  int _result = 0;
  StreamSubscription _streamSubscription;

  @override
  void initState() {
    super.initState();
    //设置一个监听  订阅一个监听
    _streamSubscription = abStreamController.stream.listen((event) {
      _result = event;
      setState(() {});
    });
  }

  @override
  void dispose() {
    //取消订阅
    _streamSubscription.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    //
    return Container(
      color: Colors.blue,
      child: Text("A中的数据 ${_result}"),
    );
  }

  Container buildContainer() {
    return Container(
      color: Colors.blue,
      child: StreamBuilder(
        stream: abStreamController.stream,
        initialData: _result,
        builder: (BuildContext context, AsyncSnapshot<int> snapshot) {
          _result = snapshot.data;
          return Text("A中的数据 ${snapshot.data}");
        },
      ),
    );
  }
}
